# 标准返回
from enum import Enum, unique
from pydantic import BaseModel
from typing import List


@unique
class responseCode(Enum):
    # 成功
    Success = 200
    # 报错
    Error = 400
    # token失效跳转登录页
    refreshToken = 401


# 返回的字典
response_code = {}
for code in responseCode:
    response_code[code.name] = code.value


class ApiResponse:
    def __init__(self, data=None, message=None, code=responseCode.Success.value):
        self.code = code
        self.data = data
        self.message = message

    def set_api_dict(self):
        api_dict = {
            "code": self.code,
            "data": self.data,
            "message": self.message
        }
        return api_dict


# 自定义返回
class CustomApiResponse(BaseModel):
    code: str | int = responseCode.Success.value
    data: dict | list = {}
    message: str | None = 'Success'

    # 自定义分页返回结构


class CustomPageResponse:
    @classmethod
    def pageBack(cls, list: List = [], total: int = 0, pagesize: int = 10):
        if total == 0 or len(list) == 0:
            return {
                'total': 0,
                'list': [],
                'total_pages': 0,
            }
        total_pages: int = -(-total // pagesize) if -(-total // pagesize) is not None else 0
        return {
            'total': total,
            'list': list,
            'total_pages': total_pages,
        }
